package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsEstimatedArrivalOrderDetailed;
import com.jy.wms.pojo.WmsPalletSorting;
import com.jy.wms.service.WmsEstimatedArrivalOrderDetailedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsEstimatedArrivalOrderDetailedController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Wed Sep 27 09:49:11 CST 2017
 */
@Controller
@RequestMapping("/wmsEstimatedArrivalOrderDetailed")
@Api(value = "/wmsEstimatedArrivalOrderDetailed", description = "关于预期到货通知单明细操作")
public class  WmsEstimatedArrivalOrderDetailedController extends BaseController<WmsEstimatedArrivalOrderDetailedService,WmsEstimatedArrivalOrderDetailed> {

    @ApiOperation(value = "ASN码盘 GET请求查询ASN码盘信息",response = JSONObject.class, notes = "GET请求查询ASN码盘信息")
    @PostMapping(value = "/seackASNSorting")
    public @ResponseBody JSONObject getSeackASNSorting(/*String ssyqdid, */@RequestBody Map<String,String> map, HttpSession session) {
        //List<WmsEstimatedArrivalOrderDetailed> tree = new ArrayList<WmsEstimatedArrivalOrderDetailed>();
        Page<WmsEstimatedArrivalOrderDetailed> tree=null;
        JSONObject json = getJSONObject();
        try {
            //tree = baseService.seachASNBySsyqdid(map.get("ssyqdid"),map.get("page"),map.get("rows"));
           int  page=Integer.parseInt(map.get("page"));
           int rows=Integer.parseInt(map.get("rows"));
            tree = baseService.seachASNBySsyqdid(map.get("ssyqdid"),page,rows);
            json = SetStatus.setSearchType(JSONArray.fromObject(tree).toString(),(int) tree.getTotal());
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return SetStatus.setSearchType(JSONArray.fromObject(tree).toString(),(int) tree.size());
    }


    @PostMapping(value = "/ConfirmationASN")
    @ApiOperation(value = "ASN收货 调整确认收货", response = JSONObject.class, notes = "调整确认收货")
    public @ResponseBody
    JSONObject getFindMaPan(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = new JSONObject();
        WmsEstimatedArrivalOrderDetailed entity = new WmsEstimatedArrivalOrderDetailed();
        try {
            entity = baseService.ConfirmationASN(setModel.getId());
            JSONObject jsonObject3 = new JSONObject();
            if(entity.getKwid()!=null&& entity.getKwmc()!="") {
                jsonObject3.accumulate("id", entity.getKwid());
                jsonObject3.accumulate("text", entity.getKwmc());
            }else{
                jsonObject3.accumulate("id", "");
                jsonObject3.accumulate("text", "");
            }
            String str= com.alibaba.fastjson.JSONObject.toJSONString(entity);
            json=JSONObject.fromObject(str);
            json=JSONObject.fromObject(entity);
            json.accumulate("telephone1",jsonObject3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    @ApiOperation(value = "预期到货通知单明细 新增预期到货通知单明细",response = JSONObject.class, notes = "POST请求新增预期到货通知单明细")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject add(HttpSession session,@RequestBody WmsEstimatedArrivalOrderDetailed entity){
        try {
            //获取当期用户所属货主
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                entity.setHzid(hz.get(0).getHzid());
            }
            baseService.baseInsertSelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增预期到货通知单明细成功","", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增预期到货通知单明细失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增预期到货通知单明细成功","", JSONObject.fromObject(entity).toString());
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增预期到货通知单明细失败",e.getMessage(),"");
        }
    }


  /*  @PostMapping(value = "/list/{ssyqdid}")
    @ApiOperation(value = "预期到货通知单 根据ASNid获取明细", response = JSONObject.class,
            notes = "根据ASNid获取明细 ssyqdid 所属预期到货通知单ID")
    public @ResponseBody
    JSONArray list(HttpSession session,@PathVariable("ssyqdid") String ssyqdid) {
        JSONArray json = new JSONArray();
        try {
            json = JSONArray.fromObject(baseService.getBySsyqdid(ssyqdid));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }*/
  //小新星改动
  @PostMapping(value = "/list")
  @ApiOperation(value = "预期到货通知单 根据ASNid获取明细", response = JSONObject.class,
          notes = "根据ASNid获取明细 ssyqdid 所属预期到货通知单ID")
  public @ResponseBody
  JSONArray list(HttpSession session,@RequestBody SetAndModifyModel setAndModifyModel) {
      JSONArray json = new JSONArray();
      try {
          json = JSONArray.fromObject(baseService.getBySsyqdid(setAndModifyModel.getId()));
      } catch (ServiceException e) {
          e.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return json;
  }



    @ApiOperation(value = "预期到货通知单明细 删除预期到货通知单明细",response = JSONObject.class, notes = "删除预期到货通知单明细")
    @PostMapping(value = "/delete")
    public @ResponseBody
    JSONObject delete(HttpSession session,@RequestBody SetAndModifyModel setAndModifyModel){
        try {
            baseService.delete(setAndModifyModel);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除预期到货通知单明细成功","", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除预期到货通知单明细成功",e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"删除预期到货通知单明细失败",e.getMessage(),"");
        } catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"删除预期到货通知单明细失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "预期到货通知单明细 修改预期到货通知单明细",response = JSONObject.class, notes = "POST请求修改预期到货通知单明细")
    @PostMapping(value = "/edit")
    public @ResponseBody
    JSONObject edit(HttpSession session,@RequestBody WmsEstimatedArrivalOrderDetailed entity){
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改预期到货通知单明细成功","", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改预期到货通知单明细成功",e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改预期到货通知单明细失败",e.getMessage(),"");
        } catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改预期到货通知单明细失败",e.getMessage(),"");
        }
    }
    /////////////////导入预期到货通知单///////////////////////////////
    /**
     * 导入预期到货通知单
     * @param session
     * @param
     * @return
     */
    @ApiOperation(value = "导入预期到货通知单", response = JSONObject.class, notes = "导入预期到货通知单")
    @PostMapping(value = "importWmsEstimatedExcle")
    public @ResponseBody
    JSONObject importWmsEstimatedExcle(HttpSession session, @RequestBody String filepath) {
        try {
            baseService.importWmsEstimatedExcle(session,filepath);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "导入预期到货通知单成功", "", "");
        }catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "导入预期到货通知单失败", e.getMessage(), "");
        }catch (RuntimeException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "导入预期到货通知单失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "导入预期到货通知单失败", e.getMessage(), "");
        }
    }
}
